package com.doyens.gmall.all.controller;

import com.doyens.gmall.cart.client.CartFeignClient;
import com.doyens.gmall.common.result.Result;
import com.doyens.gmall.model.product.SkuInfo;
import com.doyens.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller
public class CartController {
    @Autowired
    private CartFeignClient cartFeignClient;

    @Autowired
    private ProductFeignClient productFeignClient;
    @RequestMapping("cart.html")
    public String index(){

        return "cart/index";
    }

    @RequestMapping("addCart.html")
    public String addCart(@RequestParam(name = "skuId") Long skuId, @RequestParam(name = "skuNum") Integer skuNum, HttpServletRequest request){
        //远程调用cart，完成添加购物车
        cartFeignClient.addToCart(skuId,skuNum);
        //在添加购物车成功页面显示成功消息
        SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
        request.setAttribute("skuInfo",skuInfo);
        request.setAttribute("skuNum",skuNum);
        return  "cart/addCart";
    }
}
